﻿namespace Cellfish.Toolbox.Facts.Tasks
{
    using System;
    using System.Threading.Tasks;

    using Cellfish.Test;
    using Cellfish.Toolbox.Tasks;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class WithResultExtensionsFacts
    {
        [TestMethod]
        public void Given_a_null_task_When_adding_result_Then_ArgumentNullException_is_thrown()
        {
            Task task = null;
            Ensure.Throws<ArgumentNullException>(() => task.WithResult<bool>());
        }

        [TestMethod]
        public void Given_a_task_When_adding_result_Then_task_returns_result()
        {
            var task = Task.Delay(TimeSpan.Zero);
            Assert.AreEqual(default(bool), task.WithResult<bool>().Result);
        }

        [TestMethod]
        public void Given_a_faulted_task_When_adding_result_Then_task_is_faulted()
        {
            var task = Task.Factory.StartNew(() => { throw new Exception(); });
            Ensure.TaskFails<Exception>(task.WithResult<bool>());
        }
    }
}
